import logging.handlers
import os
from selenium import webdriver


FILE_PATH = os.path.abspath(os.path.dirname(__file__))

# 浏览器驱动工具类
class DriverUtils:

    # 定义全局私有化变量
    __driver = None

    @classmethod
    def open_driver(cls):
        """
        动作：打开浏览器，最大化窗口，隐式等待30秒
        :return: 浏览器对象
        """
        cls.__driver = webdriver.Chrome()
        cls.__driver.maximize_window()
        cls.__driver.implicitly_wait(30)
        return cls.__driver

    @classmethod
    def close_driver(cls):
        """
        动作：关闭浏览器，清除全局变量
        """
        if cls.__driver is not None:
            cls.__driver.quit()
            cls.__driver = None


# 日志基础配置
def log_basicConfig():
    # 创建日志器,设置日志级别
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    # 创建处理器
    ls = logging.StreamHandler()
    lht = logging.handlers.TimedRotatingFileHandler(filename='./log/HM_TEST.log',
                                                    when='midnight', interval=1,
                                                    backupCount=2, encoding="utf-8")

    # 创建格式化器
    formatter = logging.Formatter(
        fmt="%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s")
    # 给处理器添加格式化器
    ls.setFormatter(formatter)
    lht.setFormatter(formatter)
    # 给日志器添加处理器
    logger.addHandler(ls)
    logger.addHandler(lht)